//
//  AppDelegate.swift
//  QXWeibo
//
//  Created by QinXin on 15/6/23.
//  Copyright © 2015年 labi3285_Lab. All rights reserved.
//

import UIKit
import AFNetworking

// 全局的账户对象（注意要放在 @UIApplicationMain 上面）
var sharedAccount = QXAccount.loadAccount()

let QXAppDelegateChangeRootViewControllerNotification = "QXAppDelegateChangeRootViewController"

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        
//        showRunLog()
        
        setupAppearance()
        setupNetWorking()
        
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        
        window?.backgroundColor = UIColor.whiteColor()
        
        window?.makeKeyAndVisible()
        
//        window?.rootViewController = QXMainTabVC()
//        window?.rootViewController = QXGuideCellectionVC()
//        window?.rootViewController = QXWelcomeVC()
        window?.rootViewController = defaultViewController()
        
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "changeViewController:", name: QXAppDelegateChangeRootViewControllerNotification, object: nil)
        
        return true
    }
    
    func changeViewController(notice: NSNotification) {
        let showMainVc = notice.object as! Bool
        window?.rootViewController = showMainVc ? QXMainTabVC() : QXWelcomeVC()
    }
    
    private func defaultViewController() -> UIViewController {
        if sharedAccount != nil {
            return isNewUpdate() ? QXGuideCellectionVC() : QXWelcomeVC()
        }
        return QXMainTabVC()
    }
    
    private func setupAppearance() {
        UINavigationBar.appearance().tintColor = UIColor.orangeColor()
        UITabBar.appearance().tintColor = UIColor.orangeColor()
    }
    
    private func setupNetWorking() {
        // 设置网路指示器
        AFNetworkActivityIndicatorManager.sharedManager().enabled = true
        // 设置网络缓存
        let urlCache = NSURLCache(memoryCapacity: 4 * 1024 * 1024, diskCapacity: 20 * 1024 * 1024, diskPath: nil)
        NSURLCache.setSharedURLCache(urlCache)
    }
    
    private func isNewUpdate() -> Bool {
        
        let currentVersionString = NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] as! String
        let currentVersion = NSNumberFormatter().numberFromString(currentVersionString)!.doubleValue
        
        let lastVersion = NSUserDefaults.standardUserDefaults().doubleForKey("lastVersion")
        
        NSUserDefaults.standardUserDefaults().setDouble(currentVersion, forKey: "lastVersion")
        NSUserDefaults.standardUserDefaults().synchronize()
        
        return currentVersion > lastVersion
        
    }
    
    func showRunLog() {
        print("沙盒路径: " + NSHomeDirectory())
        print("账户路径: " + NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true).last!.stringByAppendingPathComponent("account.plist"))
        print("账户信息: " + "\(sharedAccount)")

    }

}

